package com.cskaoyan.order.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.constant.SysRetCodeConstants;
import com.cskaoyan.mall.commons.exception.BizException;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.order.constant.OrderRetCode;
import com.cskaoyan.order.dto.OrderDetailRequest;
import com.cskaoyan.order.dto.OrderDetailResponse;
import com.cskaoyan.order.dto.OrderListRequest;
import com.cskaoyan.order.dto.OrderListResponse;
import com.cskaoyan.order.form.OrderDetail;
import com.cskaoyan.order.form.PageResponse;
import com.cskaoyan.order.service.OrderQueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @description：
 * @author: 曹旭
 * @date: 2022/5/22 03:38
 */
@RestController
@RequestMapping("shopping")
public class OrderQueryController {

    @Autowired
    OrderQueryService orderQueryService;

    @Autowired
    HttpServletRequest httpRequest;

    @GetMapping("order")
    public ResponseData orderList(OrderListRequest request) {
        String userInfo = httpRequest.getHeader("user_info");
        JSONObject object = JSON.parseObject(userInfo);
        if(object == null){
            throw new BizException(SysRetCodeConstants.GET_TOKEN_FAILED.getCode(), SysRetCodeConstants.GET_TOKEN_FAILED.getMessage());
        }
        Long uid = Long.parseLong(object.get("id").toString());
        request.setUserId(uid);

        OrderListResponse response = orderQueryService.orderList(request);
        if (OrderRetCode.SUCCESS.getCode().equals(response.getCode())) {
            PageResponse pageResponse = new PageResponse();
            pageResponse.setData(response.getData());
            pageResponse.setTotal(response.getTotal());
            return new ResponseUtil().setData(pageResponse,"success");
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    @GetMapping("order/{id}")
    public ResponseData orderDetail(@PathVariable String id) {

        String userInfo = httpRequest.getHeader("user_info");
        JSONObject object = JSON.parseObject(userInfo);
        if(object == null){
            throw new BizException(SysRetCodeConstants.GET_TOKEN_FAILED.getCode(), SysRetCodeConstants.GET_TOKEN_FAILED.getMessage());
        }
        String username = object.get("username").toString();

        OrderDetailRequest orderDetailRequest = new OrderDetailRequest();
        orderDetailRequest.setOrderId(id);
        OrderDetailResponse response = orderQueryService.orderDetail(orderDetailRequest);
        if (OrderRetCode.SUCCESS.getCode().equals(response.getCode())) {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setUserName(username);// TODO 测试
            orderDetail.setOrderTotal(response.getPayment());
            orderDetail.setUserId(response.getUserId());
            orderDetail.setGoodsList(response.getOrderItemDto());
            orderDetail.setTel(response.getOrderShippingDto().getReceiverPhone());
            orderDetail.setStreetName(response.getOrderShippingDto().getReceiverAddress());
            orderDetail.setOrderStatus(response.getStatus());
            return new ResponseUtil().setData(orderDetail,"success");
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

}

